package com.ilop.sthome.ui.adapter.room;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.ui.adapter.device.DeviceChildAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019-2-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：房间适配器
 */
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.ItemHolder> {

    private Context mContext;
    private List<RoomBean> mList;

    public RoomAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<RoomBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public List<RoomBean> getList() {
        return mList;
    }

    public void notifyItemRefresh(String roomId){
        for (RoomBean room:mList) {
            if (roomId.equals(room.getRid())){
                notifyItemChanged(mList.indexOf(room));
                break;
            }
        }
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_room, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        RoomBean roomBean = mList.get(i);
        DeviceChildAdapter mAdapter = new DeviceChildAdapter(mContext);
        itemHolder.itemRoomList.setLayoutManager(new GridLayoutManager(mContext, 2));
        itemHolder.itemRoomList.setAdapter(mAdapter);
        if ("198".equals(roomBean.getRid())){
            List<DeviceInfoBean> mDevice = DeviceDaoUtil.getInstance().findAllShareDevice();
            String deviceNum = String.format(mContext.getString(R.string.ld_devices), String.valueOf(mDevice.size()));
            itemHolder.itemRoomName.setText(mContext.getString(R.string.ali_gateway_share));
            itemHolder.itemRoomNum.setText(deviceNum);
            mAdapter.setList(mDevice);
        }else {
            List<DeviceInfoBean> mDevice = DeviceDaoUtil.getInstance().findDeviceIntoRoom(roomBean.getRid());
            if (mDevice.size() == 0){
                itemHolder.setVisibility(false);
            }else {
                itemHolder.setVisibility(true);
                String deviceNum = String.format(mContext.getString(R.string.ld_devices), String.valueOf(mDevice.size()));
                itemHolder.itemRoomName.setText(roomBean.getNickname() == null ? ConfigureUtil.roomName(mContext, roomBean.getRid()) : roomBean.getNickname());
                itemHolder.itemRoomNum.setText(deviceNum);
                mAdapter.setList(mDevice);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_room_name)
        TextView itemRoomName;
        @BindView(R.id.item_room_num)
        TextView itemRoomNum;
        @BindView(R.id.item_room_list)
        RecyclerView itemRoomList;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        private void setVisibility(boolean visible) {
            RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            if (visible) {
                param.height = LinearLayout.LayoutParams.WRAP_CONTENT; // 根据具体需求场景设置
                param.width = LinearLayout.LayoutParams.MATCH_PARENT;
                itemView.setVisibility(View.VISIBLE);
            } else {
                itemView.setVisibility(View.GONE);
                param.height = 1;
                param.width = 0;
            }
            itemView.setLayoutParams(param);
        }
    }
}
